#include "StdFile.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/stat.h>
#define true 1
#define false 0


int IsFileExist(const char *filePath)
{
    FILE *file=fopen(filePath,"r");
    if(file!=NULL)
    {
        fclose(file);
        return true;
    }
    return false;
}

void CreateFile(const char *filepath)
{
    FILE *file=fopen(filepath,"r");
    if(file!=NULL)
    {
        printf("file is exist!\n");
        fclose(file);
        return;
    }
    file=fopen(filepath,"w");
    fclose(file);
    return;
}

void CreateFloder(const char *folderPath)
{
    int result = mkdir(folderPath,0777);
    if(result==0)
    {
        printf("create floder %s success!\n",folderPath);
        return;
    }
    else
    {
        printf("create floder %s fail!\n",folderPath);
        return;
    }
}

void RemoveFile(const char *filePath)
{
    char temp[100]={0};
    sprintf(temp,"rm -f %s",filePath);
    system(temp);
}

void CopyFile(const char *oldPath, const char *newPath)
{
    if(IsFileExist(oldPath)==false)
    {
        printf("copyfile is not exist!\n");
        return;
    }
    char *content=LoadFromFile(oldPath);
    if(IsFileExist(newPath)==true)
    {
        char temp[100]={0};
        char temp2[100]={0};
        strcpy(temp2,newPath);
        char *filename=strtok(temp2,".");
        char *backname=strtok(NULL,".");
        strcpy(temp,filename);
        strcat(temp,"_new.");
        strcat(temp,backname);
        SaveToFile(temp,content,strlen(content));
    }
    SaveToFile(newPath,content,strlen(content));
    free(content);
}

char *LoadFromFile(const char *filepath)
{
    FILE *file=fopen(filepath,"r");
    if(file==NULL)
    {
        printf("open file error! path:%s \n",filepath);
        return 0;
    }

    fseek(file,0,SEEK_END);
    long size=ftell(file);

    fseek(file,0,SEEK_SET);
    char *content=(char *)malloc(size);
    if(content==NULL)
    {
        printf("LodeFromFile malloc error!\n");
        fclose(file);
        return NULL;
    }
    fread(content,size,1,file);
    fclose(file);
    return content;
}

DLlist *GetLineFromFile(const char *filepath)
{
    FILE *file=fopen(filepath,"r");
    if(file==NULL)
    {
        printf("open file error! path:%s \n",filepath);
        return 0;
    }
    DLlist *list=(DLlist *)malloc(sizeof(DLlist));
    if(list==NULL)
    {
        printf("list malloc error!\n");
        return NULL;
    }
    InitDLinkList(list);
    char temp[100]={0};
    while(fgets(temp,100,file)!=NULL)
    {
        if(strcmp(temp,"\n")==0)
            continue;;
        char *lineContent=(char *)malloc(strlen(temp)+1);
        strcpy(lineContent,temp);
        InsertTail(list,lineContent);
    }
    fclose(file);
    return list;
}

void SaveToFile(const char *filepath, void *ptr, size_t size)
{
    FILE *file=fopen(filepath,"w");
    if(file==NULL)
    {
        printf("open file error! path:%s \n",filepath);
        return;
    }  
    fwrite(ptr,size,1,file);
    fclose(file); 
}

void SaveLineToFile(const char *filepath, DLlist *list)
{
    FILE *file=fopen(filepath,"w");
    if(file==NULL)
    {
        printf("open file error! path:%s \n",filepath);
        return;
    }  

    struct Node *TravelPoint=list->head;
    while(TravelPoint!=NULL)
    {
        fputs(TravelPoint->data,file);
        TravelPoint=TravelPoint->next;
    }
    fclose(file);     
}

void AppendToFile(const char *filepath, void *ptr)
{
    FILE *file=fopen(filepath,"a");
    if(file==NULL)
    {
        printf("open file error! path:%s \n",filepath);
        return;
    }   
    fputs(ptr,file);
    fclose(file);
}
